package com.master.common.utils;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;

import java.util.Collection;
import java.util.Map;

/**
 * @author Heath.Que
 * @version 1.0
 * @classname EmptyUtil
 * @description TODO
 * @Date 2021/5/31 14:39
 **/
public class EmptyUtil {

    public static Boolean isEmpty(Object object) {
        if (null == object) {
            return true;
        }

        if (object instanceof String) {
            String str = String.valueOf(object).trim();
            return str.equals("") || str.equals("null");
        }
        if (object instanceof Long) {
            return (long) object == 0L;
        }
        if (object instanceof Integer) {
            return (Integer) object == 0;
        }
        if (object instanceof Double) {
            return (double) object == 0.0;
        }
        if (object instanceof Collection) {
            return ((Collection) object).size() <= 0;
        }
        if (object instanceof Map) {
            return ((Map) object).size() <= 0 || ((Map) object).keySet().isEmpty();
        }
        if (object instanceof JSONArray) {
            return ((JSONArray) object).size() <= 0;
        }
        if (object instanceof JSONObject) {
            return ((JSONObject) object).size() <= 0 || ((JSONObject) object).keySet().isEmpty();
        }
        if (object instanceof String[]) {
            return ((String[]) object).length <= 0;
        }
        if (object instanceof Object[]) {
            return ((Object[]) object).length <= 0;
        }
        return false;
    }

    public static boolean isEmpty(Object... objects) {
        if (objects == null) {
            return true;
        }
        for (Object object : objects) {
            if (isEmpty(object)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isNotEmpty(Object... objects) {
        if (objects == null) {
            return false;
        }
        for (Object object : objects) {
            if (isEmpty(object)) {
                return false;
            }
        }
        return true;
    }


    public static boolean compare(Object object1, Object object2) {
        return TypeConvertUtil.valueOfString(object1).equals(TypeConvertUtil.valueOfString(object2));
    }

}

